
const {registerApp,offlineApp} = require('../services/socket')

const prefix = 'electron$';
const appPrefix = 'app$';
const loginNameMapSocket = {

}
module.exports = function (io) {
  io.on("connection", (socket) => {
    /* … */
    socket.on('online',online.bind(socket))
    socket.on('offline',offline.bind(socket))
    socket.on('disconnect',disconnect.bind(socket))
    socket.on('daka',daka.bind(socket))
  });

  function online(config) {
    loginNameMapSocket[prefix+config.erp] = this
    console.log(`${config.erp}上线了`)
    this.emit(`back_${config.erp}`,'注册成功')
  }

  function offline(config) {
    console.log(config)
    delete loginNameMapSocket[prefix+config.erp]
    io.emit('offline', config.erp)
  }

  function disconnect (){
    let disconnected = false,
      disconnectName;
    for (let key in loginNameMapSocket) {
      if (loginNameMapSocket[key].id === this.id) {
        delete loginNameMapSocket[key]
        disconnected = true
        disconnectName = key
        break
      }
    }
    console.log(disconnectName+'下线了')
    io.emit('offline', disconnectName)
  }

  function daka(config) {
    console.log('daka:'+config.erp)
    io.emit(`daka_${config.erp}`)
  }
}
